import os
from paniko import Kaniko, KanikoSnapshotMode

# 获取节点参数
dockerfile = os.getenv("JIANMU_DOCKER_FILE")
image_name = os.getenv("JIANMU_IMAGE_NAME")
image_tag = os.getenv("JIANMU_IMAGE_TAG")
# default https://index.docker.io/v1/
registry_address = os.getenv("JIANMU_REGISTRY_ADDRESS")
docker_build_path = os.getenv("JIANMU_DOCKER_BUILD_PATH")
docker_username = os.getenv("JIANMU_REGISTRY_USERNAME")
docker_password = os.getenv("JIANMU_REGISTRY_PASSWORD")
docker_build_args = os.getenv("JIANMU_DOCKER_BUILD_ARGS")
insecure = os.getenv("JIANMU_INSECUREY")
skip_tls_verify = os.getenv("JIANMU_SKIP_TLS_VERIFY")
ka = Kaniko()
# 传递参数
ka.docker_registry_uri = registry_address
ka.destination = "{0}:{1}".format(image_name, image_tag)
if docker_build_args:
    ka.build_args = docker_build_args.split(",")
ka.dockerfile = dockerfile
ka.snapshot_mode = KanikoSnapshotMode.full
if not docker_username:
    docker_username = "username"
    docker_password = "password"
ka.registry_username = docker_username
ka.registry_password = docker_password
ka.force = True
ka.context = docker_build_path
ka.insecure = insecure
ka.skip_tls_verify = skip_tls_verify

print("开始构建镜像: {0}".format("{0}:{1}".format(image_name, image_tag)))

# build
ka_log = ka.build()

print("镜像{0}已构建成功并推送。".format("{0}:{1}".format(image_name, image_tag)))
